package com.util;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class IOHelper {

	public static void copy(Reader in, Writer out) throws IOException {
		int c = -1;
		while ((c = in.read()) != -1) {
			out.write(c);
		}
	}

	public static String readFile(File file) throws IOException {
		if (file != null && file.canRead()) {
			Reader in = new FileReader(file);
			return readFile(in);
		}
		return "";
	}
	
	public static String readFile(InputStream is) throws IOException {
		Reader in = new InputStreamReader(is);
		
		return readFile(in);
	}
	
	public static String readFile(Reader reader) throws IOException {
		if (reader != null) {
			StringWriter out = new StringWriter();
			copy(reader, out);
			return out.toString();
		}
		return "";
	}

	public static void saveFile(File file, String content) throws IOException {
		Writer writer = new FileWriter(file);
		writer.write(content);
		writer.close();
	}

}